// GUI控制界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

class MusicPlayerGUI {
    private final Playlist playlist;
    private final Player player;
    private JFrame frame;
    private JList<AudioFile> playlistJList;
    private DefaultListModel<AudioFile> listModel;
    private JLabel currentTrackLabel;

    public MusicPlayerGUI(Playlist playlist, Player player) {
        this.playlist = playlist;
        this.player = player;
    }

    public void createAndShowGUI() {
        frame = new JFrame("简易音乐播放器");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLayout(new BorderLayout());

        // 播放列表
        listModel = new DefaultListModel<>();
        playlistJList = new JList<>(listModel);
        playlistJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane playlistScrollPane = new JScrollPane(playlistJList);
        frame.add(playlistScrollPane, BorderLayout.CENTER);

        // 当前播放曲目标签
        currentTrackLabel = new JLabel("当前播放: 无");
        frame.add(currentTrackLabel, BorderLayout.NORTH);

        // 控制按钮面板
        JPanel controlPanel = new JPanel();
        
        JButton playButton = new JButton("播放");
        JButton pauseButton = new JButton("暂停");
        JButton stopButton = new JButton("停止");
        JButton nextButton = new JButton("下一首");
        JButton prevButton = new JButton("上一首");
        JButton addButton = new JButton("添加文件");
        JButton removeButton = new JButton("移除");
        
        controlPanel.add(playButton);
        controlPanel.add(pauseButton);
        controlPanel.add(stopButton);
        controlPanel.add(prevButton);
        controlPanel.add(nextButton);
        controlPanel.add(addButton);
        controlPanel.add(removeButton);

        // 音量控制
        JSlider volumeSlider = new JSlider(0, 100, 50);
        JLabel volumeLabel = new JLabel("音量:");
        controlPanel.add(volumeLabel);
        controlPanel.add(volumeSlider);

        frame.add(controlPanel, BorderLayout.SOUTH);

        // 事件监听器 - 播放按钮
        playButton.addActionListener(e -> {
            int selectedIndex = playlistJList.getSelectedIndex();
            if (selectedIndex != -1) {
                playlist.setCurrentIndex(selectedIndex);
                player.play(playlist.getCurrent());
                updateCurrentTrackLabel();
            } else if (playlist.getCurrentIndex() != -1) {
                player.play(playlist.getCurrent());
                updateCurrentTrackLabel();
            }
        });

        // 事件监听器 - 暂停按钮
        pauseButton.addActionListener(e -> player.pause());

        // 事件监听器 - 停止按钮
        stopButton.addActionListener(e -> {
            player.stop();
            updateCurrentTrackLabel();
        });

        // 事件监听器 - 下一首按钮
        nextButton.addActionListener(e -> {
            if (playlist.getSize() > 0) {
                player.play(playlist.next());
                playlistJList.setSelectedIndex(playlist.getCurrentIndex());
                updateCurrentTrackLabel();
            }
        });

        // 事件监听器 - 上一首按钮
        prevButton.addActionListener(e -> {
            if (playlist.getSize() > 0) {
                player.play(playlist.previous());
                playlistJList.setSelectedIndex(playlist.getCurrentIndex());
                updateCurrentTrackLabel();
            }
        });

        // 事件监听器 - 添加文件按钮
        addButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setMultiSelectionEnabled(true);
            fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
                @Override
                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().toLowerCase().endsWith(".wav") ||
                           f.getName().toLowerCase().endsWith(".au") ||
                           f.getName().toLowerCase().endsWith(".aiff");
                }

                @Override
                public String getDescription() {
                    return "音频文件 (*.wav, *.au, *.aiff)";
                }
            });

            int result = fileChooser.showOpenDialog(frame);
            if (result == JFileChooser.APPROVE_OPTION) {
                File[] selectedFiles = fileChooser.getSelectedFiles();
                for (File file : selectedFiles) {
                    AudioFile audioFile = new AudioFile(file);
                    playlist.add(audioFile);
                    listModel.addElement(audioFile);
                }
            }
        });

        // 事件监听器 - 移除按钮
        removeButton.addActionListener(e -> {
            int selectedIndex = playlistJList.getSelectedIndex();
            if (selectedIndex != -1) {
                playlist.remove(selectedIndex);
                listModel.remove(selectedIndex);
                if (playlist.getSize() > 0) {
                    playlistJList.setSelectedIndex(Math.min(selectedIndex, playlist.getSize() - 1));
                }
                updateCurrentTrackLabel();
            }
        });

        // 事件监听器 - 音量滑块
        volumeSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            if (!source.getValueIsAdjusting()) {
                float volume = (float) source.getValue() / 100.0f;
                player.setVolume(volume);
            }
        });

        frame.setVisible(true);
    }

    private void updateCurrentTrackLabel() {
        AudioFile current = playlist.getCurrent();
        if (current != null) {
            currentTrackLabel.setText("当前播放: " + current.getTitle());
        } else {
            currentTrackLabel.setText("当前播放: 无");
        }
    }
}